/*
 * @Author: liu.lin liu.lin@hzleaper.com
 * @Date: 2024-03-25 09:01:57
 * @LastEditors: liu.lin liu.lin@hzleaper.com
 * @LastEditTime: 2024-04-12 09:16:05
 * @FilePath: \nest_temp\src\main.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import { TestInterceptor } from './interceptor/response.interceptor';
import { Request, Response, NextFunction } from 'express';
import * as cors from 'cors';
import { NestExpressApplication } from '@nestjs/platform-express';
import path, { join } from 'path';
import { HttpFilter } from './interceptor/exception.interceptor';
import * as winston from 'winston';
import { createLogger } from 'winston';
import { utilities, WinstonModule } from 'nest-winston';
import 'winston-daily-rotate-file'; // 用于存储日志到文件

const whitelist = ['/test', '/test2', '/', '/jj', '/upload/ablum'];
function MiddlewareAll(req: Request, res: Response, next: NextFunction) {
  console.log(`req`, req.originalUrl);
  // if (whitelist.includes(req.originalUrl)) {
  //   next();
  // } else {
  //   res.send('no permission');
  // }
  next();
}

async function bootstrap() {
  // 日志配置
  const instance = createLogger({
    // 日志选项
    transports: [
      new winston.transports.Console({
        level: 'info',
        // 字符串拼接
        format: winston.format.combine(
          winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), // 设置自定义时间格式
          utilities.format.nestLike(),
        ),
      }),
      // warn、error日志存储到/logs/application-日期.log文件中
      new winston.transports.DailyRotateFile({
        level: 'warn',
        dirname: 'logs',
        filename: 'error-%DATE%.log',
        datePattern: 'YYYY-MM-DD-HH',
        zippedArchive: true,
        maxSize: '20m',
        maxFiles: '14d',
        format: winston.format.combine(
          winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), // 设置自定义时间格式,
          winston.format.simple(),
        ),
      }),
      // info日志存储到/logs/info-日期.log文件中
      new winston.transports.DailyRotateFile({
        level: 'info',
        dirname: 'logs',
        filename: 'info-%DATE%.log',
        datePattern: 'YYYY-MM-DD-HH',
        zippedArchive: true,
        // 文件大小
        maxSize: '20m',
        // 最多14 天
        maxFiles: '14d',
        format: winston.format.combine(
          winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), // 设置自定义时间格式,
          winston.format.simple(),
        ),
      }),
    ],
  });

  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    logger: WinstonModule.createLogger({
      instance,
    }),
  });
  console.log(`__dirname`, __dirname);

  app.useStaticAssets(join(__dirname, '../imges'));
  app.use(cors());
  app.useGlobalInterceptors(new TestInterceptor(new Reflector()));
  app.use(MiddlewareAll);
  app.useGlobalFilters(new HttpFilter());
  await app.listen(3000);
}
bootstrap();
